package Servlet.Teacher;

import Entity.MsgAndEmail.Email;
import Entity.MyTableData;
import Service.Teacher.Imp.LoginServiceImp;
import Service.Teacher.Imp.MsgAndEmailServiceImp;
import Service.Teacher.LoginService;
import Service.Teacher.MsgAndEmailService;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by IntelliJ IDEA.
 * Author: Deng PengFei
 * Date: 2021/8/22
 * Time: 14:07
 */

@WebServlet(urlPatterns = {"/getMsg", "/getEmail", "/writeEmail", "/updateEmail", "/updateUserEmail", "/updateEmailCode"},name = "MsgAndEmailServlet")
public class MsgAndEmailServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String url = request.getRequestURI();
        if ("/getMsg".equals(url)) {
            getMsg(request, response);
        } else if ("/getEmail".equals(url)) {
            getEmail(request, response);
        } else if ("/writeEmail".equals(url)) {
            writeEmail(request, response);
        } else if ("/updateEmail".equals(url)) {
            updateEmail(request, response);
        } else if ("/updateUserEmail".equals(url)) {
            updateUserEmail(request, response);
        } else if ("/updateEmailCode".equals(url)) {
            updateEmailCode(request, response);
        }
    }


    /**
     * 用户修改邮箱的授权码
     */
    private void updateEmailCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        MsgAndEmailService msgAndEmailService = new MsgAndEmailServiceImp();
        String NO = request.getParameter("userName");
        String role = request.getParameter("role");
        String emailCode = request.getParameter("emailCode");
        Boolean flag = msgAndEmailService.updateEmailCode(NO, emailCode, role);
        PrintWriter out = response.getWriter();
        out.print(flag);
        out.close();
    }

    /**
     * 用户修改邮箱
     */
    private void updateUserEmail(HttpServletRequest request, HttpServletResponse response) throws IOException {
        MsgAndEmailService msgAndEmailService = new MsgAndEmailServiceImp();
        String NO = request.getParameter("userName");
        String role = request.getParameter("role");
        String newEmail = request.getParameter("email");
        Boolean flag = msgAndEmailService.updateUserEmail(NO, newEmail, role);
        PrintWriter out = response.getWriter();
        out.print(flag);
        out.close();
    }


    /**
     * 获取所有的公告
     *
     * @param request  请求
     * @param response 响应
     * @throws ServletException 异常
     * @throws IOException      异常
     */
    protected void getMsg(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        MsgAndEmailService msgAndEmailService = new MsgAndEmailServiceImp();
        PrintWriter out = response.getWriter();
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int offset = (page - 1) * limit;
        String queryKey = request.getParameter("queryKey");
        if (queryKey == null || "".equals(queryKey)) {
            queryKey = "";
        }
        queryKey = "%" + queryKey + "%";
        MyTableData myTableData = msgAndEmailService.getAllMessage(queryKey, offset, limit);
        String json = JSONObject.toJSONString(myTableData, SerializerFeature.WriteMapNullValue);
        out.print(json);
        out.close();
    }

    /**
     * 获取所有的邮箱内容 type=1是收件箱  type=2发件箱
     *
     * @param request  请求
     * @param response 响应
     * @throws ServletException 异常
     * @throws IOException      异常
     */
    protected void getEmail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        MsgAndEmailService msgAndEmailService = new MsgAndEmailServiceImp();
        PrintWriter out = response.getWriter();
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int offset = (page - 1) * limit;
        int type = Integer.parseInt(request.getParameter("type"));
        String NO = request.getParameter("userName");
        String queryKey = request.getParameter("queryKey");
        if (queryKey == null || "".equals(queryKey)) {
            queryKey = "";
        }
        queryKey = "%" + queryKey + "%";
        MyTableData myTableData = msgAndEmailService.getAllEmail(queryKey, offset, limit, NO, type);
        String json = JSONObject.toJSONString(myTableData, SerializerFeature.WriteMapNullValue);
        out.print(json);
        out.close();
    }

    /**
     * 发送一封邮件
     *
     * @param request  请求
     * @param response 响应
     * @throws ServletException 异常
     * @throws IOException      异常
     */
    protected void writeEmail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        MsgAndEmailService msgAndEmailService = new MsgAndEmailServiceImp();
        PrintWriter out = response.getWriter();
        String NO = request.getParameter("userName");
        String data = request.getParameter("emailData");
        JSONObject json = JSONObject.parseObject(data);
        Integer role = Integer.parseInt(json.get("role").toString());
        Email email = new Email();
        email.setSendNo(NO);
        email.setTitle(json.get("title").toString());
        email.setReceiveNo(json.get("NO").toString());
        email.setContent(json.get("text").toString());
        out.print(msgAndEmailService.writeEmail(email, role));
        out.close();
    }

    /**
     * 更新邮箱是否读取的状态
     *
     * @param request  请求
     * @param response 响应
     * @throws ServletException 异常
     * @throws IOException      异常
     */
    protected void updateEmail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        MsgAndEmailService msgAndEmailService = new MsgAndEmailServiceImp();
        PrintWriter out = response.getWriter();
        Integer id = Integer.parseInt(request.getParameter("id"));
        boolean flag = msgAndEmailService.updateStateById(id);
        out.print(flag);
        out.close();
    }


}
